/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.nus.application.web.conference;

import com.nus.application.web.conference.form.ConferenceMaintenanceForm;
import com.nus.application.ejb.user.ConferenceEjbLocal;
import com.nus.application.web.base.BaseAction;
import com.nus.dataaccess.dao.conference.Conference;
import com.nus.dataaccess.dao.util.HibernateUtil;
import com.opensymphony.xwork2.ValidationAware;
import javax.inject.Inject;
import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;

/**
 *
 * @author kenny
 */
public class ConferenceMaintenance extends BaseAction implements ValidationAware{

    @Inject
    ConferenceEjbLocal conferenceEjb;

    public String doList(){
        this.form.setFullListSize(conferenceEjb.countPaginateConference(null));
        this.form.setPageNumber(super.getPage());
        System.err.println(this.getForm().getPageNumber());
        this.getForm().setConferences(
                conferenceEjb.paginateConference(form.calculateStartRow(super.getPage()), form.getObjectsPerPage(), null, false, null)
                );
        return SUCCESS;
    }
/*
    private String conferenceId;
    public void setConferenceId(String conferenceId) {
        System.out.println("test");
        System.err.println("aaaa");
//        this.getForm().setConference(conferenceEjb.retrieveConferenceById(conferenceId));
    }
    public String getConferenceId() {return conferenceId;}
*/
    public String doMaintain(){
        this.validate();
        Conference conference = conferenceEjb.retrieveConferenceById(this.getForm().getConference().getId());
        this.getForm().setConference(conference);

        return SUCCESS;
    }

    public String doCreate(){
        this.getForm().setConference(new Conference());
        return SUCCESS;
    }

    public String doSave(){
        Conference temp = (Conference) HibernateUtil.getCurrentSession().merge(this.getForm().getConference());
        conferenceEjb.persistConference(temp);
        super.addActionMessage("Information successfully saved");
        return SUCCESS;
    }
    
    public String doPublish(){
        conferenceEjb.publishConferenceItem(this.getForm().getConference().getId());
        super.addActionMessage("Information successfully published");
        return SUCCESS;
    }

    public String doViewSubmission(){
        Conference conference = conferenceEjb.retrieveConferenceById(this.getForm().getConference().getId());
        this.getForm().setConference(conference);
        return SUCCESS;
    }

    private ConferenceMaintenanceForm form = new ConferenceMaintenanceForm();
    public ConferenceMaintenanceForm getForm() {return form;}
    public void setForm(ConferenceMaintenanceForm form) {this.form = form;}


    @Override
    public void validate(){

        if(!super.isSaveEvent()){
            if(super.isListEvent())
                super.clearActionErrors();

            return;
        }

        if(StringUtils.isEmpty(this.getForm().getConference().getName()))
            super.addActionError(super.getText("error.conference_name_is_required"));

        if (StringUtils.isEmpty(this.getForm().getConference().getDescription() )) {
            super.addActionError(super.getText("error.description_is_required"));
        }
        if(!super.hasActionErrors()){


        }
    }

}
